/*
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

fileManager.ui.FileGridPanel = function(dataProvider){

	var that = this;
	
	//	data source			
	this.store = new Ext.data.JsonStore({
		
		url: dataProvider,
		root: 'QUERY',
        totalProperty: 'TOTALROWCOUNT',
		fields: [
			'canread',
			'name',
			'attributes',
			'pathfrombase',
			'datelastmodified',
			'relativepath',
			'canwrite',
			'directory',
			'fullpath',
			'extension',
			'mode',
			'size',
			'type'
		]
		
	});
	
	this.columns = new Ext.grid.ColumnModel([{
			header: "Type",
			width: 40,
			dataIndex: 'extension',
			editable:false,
			sortable: true,
			renderer: function(val){
				iconFile="/fileManager/assets/images/icon/"+val+".jpg";
				iconUnk="/fileManager/assets/images/icon/unk.jpg";
				var tmp= new Image;
				tmp.src=iconFile;
				
				function FileExists(strURL)
				{
					var gotIcon = false;
					for(c=0 ; c < iconsSet.length ; c++){
						if(iconsSet[c] == val + '.jpg'){
							return true;
							break;
						}
					}
				    
				}
				
				if(FileExists(iconFile)==true){
					return '<img style="width:16px;height:16px;" src="' + iconFile + '">';
				}else{
					return '<img style="width:16px;height:16px;" src="' + iconUnk + '">';
				}
			}
		}, {
			header: "Name",
			width: 230,
			dataIndex: 'name',
			sortable: true,
			editable:true,
           	editor: new Ext.form.TextField({
               allowBlank: false
           	}),
			draggable:true
		}, {
			header: "Size",
			width: 70,
			dataIndex: 'size',
			editable:false,
			sortable: true
		}, {
			header: "Directory",
			width: 200,
			dataIndex: 'relativepath',
			editable:false,
			sortable: true
		}, {
			header: "Date last modified",
			width: 150,
			dataIndex: 'datelastmodified',
			editable:false,
			sortable: true
		}, {
		
			xtype:		'actioncolumn',
			id:			'select_item',
			width:		50,
			header:		"Select",
			editable: 	false,
			sortable: 	false,
			
			items: [{
				icon   : '/fileManager/assets/images/icon/add.gif',
                tooltip: 'Add this item',
                handler: function(grid, rowIndex, colIndex) {
                
                    var rec = grid.store.getAt(rowIndex);
                    
					// Get the choosen file fullpath
					if (that.panel) {

						that.panel.fireEvent( 'selectitem', rec );
						
					}

                }
			}]
		
		}]);
	
	this.panel = new Ext.grid.EditorGridPanel({
		
		cm: this.columns,
		
		store:this.store,
		
		height:300,
		
		maxHeight:300,
		
		enableDragDrop: true,
		
		ddGroup: 'gridDDGroup',
		
        stripeRows: true,
		
		clicksToEdit: 2,

		selModel: new Ext.grid.RowSelectionModel({singleSelect:true}),
		
		split:false,
		
		border:false,
		
		selectionButton: false,
	
		showSelectionButton: function(){
			var colModel = this.getColumnModel();
			colModel.setHidden( colModel.getIndexById( 'select_item' ), false );
		},
		hideSelectionButton: function(){
			var colModel = this.getColumnModel();
			colModel.setHidden( colModel.getIndexById( 'select_item' ), true );
		},
		
		listeners: {
			afteredit: function(e){
				
				if (e.record.data.canwrite == true) {
					
					Ext.Ajax.request({
						url: services.filemanager.renameFile,
						method: 'post',
						params: {
							filePath: "/" + e.record.data.relativepath,
							newFile: e.value
						}
					});
					
					this.store.reload({});
					
				}else{
					Ext.Msg.show({
						title: 'Error',
						msg: 'This file is under write protection.',
						buttons: Ext.Msg.OK,
						maxWidth:300,
						minWidth:300
					});
				}
			},
			
			beforerender: function(){
			
				if ( this.selectionButton ){
				
					this.showSelectionButton();
					
				} else {
				
					this.hideSelectionButton();
					
				}
				
			}
			
		},
		
		bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: this.store,
            displayInfo: true,
            displayMsg: 'Displaying Files {0} - {1} of {2}',
            emptyMsg: "No files to display"
        })

		
	});
	
	this.panel.addEvents('selectitem');	
	
	return this.panel;
	
}
